package com.yingpai.fitness.dvp_2_0.main.activity.dynamic;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.orhanobut.logger.Logger;
import com.yingpai.fitness.R;
import com.yingpai.fitness.dvp_2_0.common.activity.CommonActivity;
import com.yingpai.fitness.dvp_2_0.common.adapter.BaseAdapterHelper;
import com.yingpai.fitness.dvp_2_0.common.adapter.QuickAdapter;
import com.yingpai.fitness.dvp_2_0.common.util.constant.Contacts;
import com.yingpai.fitness.dvp_2_0.main.widget.MyListView;
import com.yingpai.fitness.dvp_2_0.main.widget.video.VideoPlayer;
import com.yingpai.fitness.dvp_2_0.main.widget.video.VideoPlayerStandard;

import java.lang.reflect.Field;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ASUS on 2019/2/25.
 */

public class DynamiVideoActivity extends CommonActivity {
    @BindView(R.id.listview)
    MyListView listview;
    @BindView(R.id.comment_count)
    TextView commentCount;
    @BindView(R.id.like_image)
    ImageView likeImage;
    @BindView(R.id.like_count)
    TextView likeCount;
    @BindView(R.id.back)
    TextView back;
    @BindView(R.id.bottom_rl)
    RelativeLayout bottom_rl;
    private int mScreenHeight;
    private QuickAdapter<String> quickAdapter;
    private int postion;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusFlag(Contacts.CONTENT_INVASION_STATUS);
        setContentView(R.layout.activity_dynamic_video);

    }

    private int getHeight(){
        try {
            Class<?> clazz = Class.forName("com.android.internal.R$dimen");
            Field field = clazz.getField("status_bar_height");
            int x = Integer.parseInt(field.get(clazz.newInstance()).toString());
            int sStatusBarHeight = context.getResources().getDimensionPixelSize(x);
            return sStatusBarHeight;
        } catch (Throwable ignored) {
        }
        return 0;
    }

    @Override
    protected void initView() {
        VideoPlayerStandard.SAVE_PROGRESS = false;

        //1:通过图片资源文件生成Drawable实例
        Drawable drawable = getResources().getDrawable(R.mipmap.back_black).mutate();
        //2:先调用DrawableCompat的wrap方法
        drawable = DrawableCompat.wrap(drawable);
        //3:再调用DrawableCompat的setTint方法，为Drawable实例进行着色
        DrawableCompat.setTint(drawable, Color.WHITE);

        back.setCompoundDrawablesWithIntrinsicBounds(drawable,
                null, null, null);

        DisplayMetrics dm = new DisplayMetrics();
        WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(dm);
        //屏幕高度
        mScreenHeight = dm.heightPixels + getHeight();

        quickAdapter = new QuickAdapter<String>(context, R.layout.item_dynamic_video) {
            @Override
            protected void convert(BaseAdapterHelper helper, String item) {
                RelativeLayout itemll = helper.getView(R.id.item_ll);
                VideoPlayerStandard video = helper.getView(R.id.video);
                AbsListView.LayoutParams params = (AbsListView.LayoutParams) itemll.getLayoutParams();
                params.height = mScreenHeight;
                itemll.setLayoutParams(params);
                Glide.with(context)
                        .load("http://c.hiphotos.baidu.com/zhidao/pic/item/562c11dfa9ec8a1359aa88b6f103918fa0ecc030.jpg")
                        .into(video.thumbImageView);

                video.setUp(item, VideoPlayer.SCREEN_WINDOW_LIST);

                video.setAutoComletionListener(new VideoPlayerStandard.AutoCompletionListener() {
                    @Override
                    public void onAutoCompletion() {
                        listview.smoothScrollToPosition(postion + 1);
                    }
                });

                if (helper.getPosition() == 0) {
                    video.positionInList = helper.getPosition();
                    postion = helper.getPosition();
                    video.startPlay();
                }
            }
        };
        listview.setAdapter(quickAdapter);

        listview.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState != AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL
                        && scrollState != AbsListView.OnScrollListener.SCROLL_STATE_FLING
                        && scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE
                        && view.getChildAt(0) != null) {
                    Logger.e("view.getChildAt(0).getTop()="+view.getChildAt(0).getTop());
                    if(view.getChildAt(0).getTop()<-50){
                        if (view.getChildAt(0).getTop() + mScreenHeight > (mScreenHeight) / 2) {
                            listview.smoothScrollToPosition(view.getFirstVisiblePosition());
                            if (postion != view.getFirstVisiblePosition()) {
                                postion = view.getFirstVisiblePosition();
                                VideoPlayerStandard video = view.getChildAt(0).findViewById(R.id.video);
                                video.positionInList = view.getFirstVisiblePosition();
                                video.startPlay();
                            }

                        } else {
                            listview.smoothScrollToPosition(view.getLastVisiblePosition());
                            if (postion != view.getLastVisiblePosition()) {
                                postion = view.getLastVisiblePosition();
                                VideoPlayerStandard video = view.getChildAt(1).findViewById(R.id.video);
                                video.positionInList = view.getLastVisiblePosition();
                                video.startPlay();
                            }

                        }
                    }
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                VideoPlayerStandard.onScrollReleaseAllVideos(view, firstVisibleItem, visibleItemCount, totalItemCount);
            }
        });

    }

    @Override
    public void onBackPressed() {
        if (VideoPlayerStandard.backPress()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        VideoPlayerStandard.releaseAllVideos();
    }

    @Override
    protected void initData() {
        quickAdapter.add("http://jzvd.nathen.cn/6ea7357bc3fa4658b29b7933ba575008/fbbba953374248eb913cb1408dc61d85-5287d2089db37e62345123a1be272f8b.mp4");
        quickAdapter.add("http://jzvd.nathen.cn/63f3f73712544394be981d9e4f56b612/69c5767bb9e54156b5b60a1b6edeb3b5-5287d2089db37e62345123a1be272f8b.mp4");
        quickAdapter.add("http://jzvd.nathen.cn/35b3dc97fbc240219961bd1fccc6400b/8d9b76ab5a584bce84a8afce012b72d3-5287d2089db37e62345123a1be272f8b.mp4");
        quickAdapter.add("http://jzvd.nathen.cn/d2438fd1c37c4618a704513ad38d68c5/68626a9d53ca421c896ac8010f172b68-5287d2089db37e62345123a1be272f8b.mp4");
        quickAdapter.add("http://jzvd.nathen.cn/d2e969f2ec734520b46ab0965d2b68bd/f124edfef6c24be8b1a7b7f996ccc5e0-5287d2089db37e62345123a1be272f8b.mp4");
        quickAdapter.add("http://jzvd.nathen.cn/f07fa9fddd1e45a6ae1570c7fe7967c1/c6db82685b894e25b523b1cb28d79f2e-5287d2089db37e62345123a1be272f8b.mp4");
        quickAdapter.notifyDataSetChanged();

    }

    @Override
    @OnClick({R.id.share, R.id.comment_rl, R.id.like_rl})
    public void onViewClicked(View view) {
        super.onViewClicked(view);
        switch (view.getId()) {
            case R.id.share:
                break;
            case R.id.comment_rl:
                break;
            case R.id.like_rl:
                showToast("postion = " + postion);
                break;
        }
    }
}
